<script setup lang="ts">
import { getMedicalOrderLogistics } from '@/services/order'
import type { Logistics } from '@/types/order'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import startImg from '@/assets/start.png'
import endImg from '@/assets/end.png'
import carImg from '@/assets/car.png'
import AMapLoader from '@amap/amap-jsapi-loader' // 导入高德地图插件
import type { Location } from '@/types/order'
// 配置全局window的安全属性  => 提供jscode
// window内置对象 有一个内置类型Window
window._AMapSecurityConfig = {
  securityJsCode: '415e917da833efcf2d5b69f4d821784b'
}

// 加载地图
const initMap = () => {
  // 加载地图
  AMapLoader.load({
    key: '4eed3d61125c8b9c168fc22414aaef7e', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '2.0' // 指定要加载的 JS API 的版本，缺省时默认为 1.4.15
  }).then((AMap) => {
    // AMap 不是直接的地图构造函数 => { Map: 地图构造函数 }
    // const map = new AMap.Map('指定地图渲染在哪个盒子里') // 实例化
    const map = new AMap.Map('map', {
      zoom: 12, // 缩放级别
      mapStyle: 'amap://styles/whitesmoke' //设置地图的显示样式
    }) // 实例化
    console.log(map)

    // 驾车规划路线
    AMap.plugin('AMap.Driving', function () {
      // 实例化驾车路线规划插件
      var driving = new AMap.Driving({
        // 驾车路线规划策略，AMap.DrivingPolicy.LEAST_TIME是最快捷模式
        policy: AMap.DrivingPolicy.LEAST_TIME,
        map: map, // 指定路线规划在哪个地图
        showTraffic: false, // 关闭交通状态  堵不堵不关心
        hideMarkers: true // 关闭默认标记  一会自定义起点终点标记
      })

      // 准备起点 终点坐标
      // var startLngLat = [116.379028, 39.865042]
      // var endLngLat = [116.427281, 39.903719]

      if (
        logistics.value?.logisticsInfo &&
        logistics.value.logisticsInfo.length >= 2
      ) {
        const list = [...logistics.value.logisticsInfo]
        const start = list.shift() // 起点
        const end = list.pop() // 终点

        const getMarker = (
          img: string,
          point?: Location,
          width: number = 25,
          height: number = 30
        ) => {
          // 创建 AMap.Icon 实例：控制大小
          const icon = new AMap.Icon({
            size: new AMap.Size(width, height), // 图标尺寸
            image: img, // Icon的图像
            imageSize: new AMap.Size(width, height) // 根据所设置的大小拉伸或压缩图片
          })

          // 添加标记
          const marker = new AMap.Marker({
            position: new AMap.LngLat(point?.longitude, point?.latitude), // 标记经纬度
            icon: icon, // 标记图标 URL
            offset: new AMap.Pixel(-width / 2, -height)
          })

          return marker
        }

        map.add(getMarker(startImg, start, 25, 30))
        map.add(getMarker(endImg, end))
        const carMarker = getMarker(
          carImg,
          logistics.value.currentLocationInfo,
          25,
          15
        )
        map.add(carMarker)

        // 路线规划(最快规划 => 添加途径点规划)
        driving.search(
          [start?.longitude, start?.latitude],
          [end?.longitude, end?.latitude],
          {
            // [ {} , {}]  [ [], [] ]
            waypoints: list.map((item) => {
              return [item.longitude, item.latitude]
            })
          },
          function () {
            console.log('规划好了')

            // 如果规划好了  渲染地图 => 等3s
            // map.setZoom(10)
            setTimeout(() => {
              map.setZoom(10) // 缩放
              map.setFitView([carMarker]) // 以小车标记当前为中心
            }, 3000)
          }
        )
      }
    })
  })
}

// window.a
// window.b
// window.location

// 获取物流信息
const logistics = ref<Logistics>()
const route = useRoute()
onMounted(async () => {
  const res = await getMedicalOrderLogistics(route.params.id as string)
  logistics.value = res.data

  // 页面加载好后 渲染地图(依赖dom)
  // ....   fn()
  initMap()
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">
          {{ logistics?.statusValue }} 预计{{ logistics?.estimatedTime }}送达
        </p>
        <p class="predict">
          <span>{{ logistics?.name }}</span>
          <span>{{ logistics?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics?.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">{{ item.content }}</p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
